package com.aqgj.Role.web;

import com.alibaba.fastjson.JSONArray;
import com.aqgj.Role.bean.Role;
import com.aqgj.Role.service.RoleManager;
import com.aqgj.common.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("role")
public class RoleAction  extends BaseController{

    @Autowired
    private RoleManager roleManager;

    /**
     * 角色管理列表
     *
     * @param pageSize
     * @param pageNumber 页码
     * @return
     */
    @RequestMapping("/dataGrid")
    @ResponseBody
    public Object dataGrid(Integer pageSize, Integer pageNumber) {
        return renderSuccess(roleManager.selectDataGrid(pageSize,pageNumber));
    }

    /**
     * 所有角色
     *
     * @return
     */
    @RequestMapping("/tree")
    @ResponseBody
    public Object tree() {
        return renderSuccess(roleManager.selectAll());
    }

    /**
     * 添加权限
     *
     * @param role
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Role role) {
        roleManager.insertRole(role);
        return renderSuccess("添加成功！");
    }

    /**
     * 删除权限
     *
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id) {
        List<String> roleList=new ArrayList<>();
        roleList.add("402880f25fe7b388015fe7c01e140036");
        roleList.add("402880f25fe7b388015fe7cc729e0047");
        roleList.add("402893815fec9fa8015feceee7e30058");
        roleList.add("402893815fec9fa8015fecef3ea90059");
        roleList.add("ff8080815f2820ad015f285971620000");
        roleList.add("4028938161f442530161f54b53730000");
        roleList.add("402880f262fbf44f0162fbf637750000");
        if(roleList.contains(id)){
            return renderError("该权限不能被删除");
        }
        roleManager.deleteById(id);
        return renderSuccess("删除成功！");
    }

    /**
     * 编辑权限
     *
     * @param role
     * @return
     */
    @RequestMapping("/edit")
    @ResponseBody
    public Object edit(Role role) {
        roleManager.insertRole(role);
        return renderSuccess("编辑成功！");
    }

    /**
     * 编辑权限页
     *
     * @param id
     * @return
     */
    @RequestMapping("/editPage")
    @ResponseBody
    public Object editPage(String id) {
        return renderSuccess(roleManager.selectRoleById(id));
    }

    /**
     * 授权
     *
     * @param id
     * @param resourceIds
     * @return
     */
    @RequestMapping("/grant")
    @ResponseBody
    public Object grant(String id, String resourceIds) {
        JSONArray jsonArray= (JSONArray) JSONArray.parse(resourceIds);
        roleManager.updateRoleResource(id, jsonArray);
        return renderSuccess("授权成功！");
    }
}